package com.mygdx.game.desktop.demo;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
/**
 * .中文测试类
 * @author Administrator
 *
 */
public class ChineseTestGame extends Game {

	SpriteBatch batch;

	BitmapFont font;
	//FreeTypeBitmapFontData fontData;
	FreeTypeFontGenerator generator;
	Label label;
	Stage stage;

	@Override
	public void create() {
		batch = new SpriteBatch();
		// 读取常用汉字表
		FileHandle intHandle = Gdx.files.internal("font/font.txt");
		String text = intHandle.readString();
		// 读取ttf并赋予常用字
		generator = new FreeTypeFontGenerator(Gdx.files.internal("font/ttf/STLITI.TTF"));
		FreeTypeFontParameter parameter = new FreeTypeFontParameter();
		parameter.characters = FreeTypeFontGenerator.DEFAULT_CHARS + text;
		font = generator.generateFont(parameter);
		generator.dispose();
		// 舞台
		stage = new Stage();
		stage.setDebugAll(true);
		// 输出文本及载体
		label = new Label(poetryStr("涛山阻绝秦帝船，汉宫彻夜捧金盘。玉肌枉然生白骨，不如剑啸易水寒。", 8), new LabelStyle(font, Color.BLACK));
		// padding right/top效果
		label.setPosition(stage.getWidth()-label.getWidth()-20f, stage.getHeight()-label.getHeight()-20f);
		// 添加演员
		stage.addActor(label);
	}

	public void render() {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT );
		stage.act();
		stage.draw();
	}

	// 诗化竖排输出汉字
	public String poetryStr(String str, int colcount) {
		String formatStr = "";
		String pointSentence;
		int length = str.length();
		String[][] charArray = new String[length/colcount][colcount];
		// 字符串按colcount存入二维数组
		for(int i=0; i<length/colcount; i++) {
			pointSentence = str.substring(i*colcount, i*colcount+colcount);
			for(int j=0; j<colcount; j++) {
				charArray[i][j] = pointSentence.charAt(j)+"";
			}
			//System.out.println(Arrays.toString(charArray[i]));
		}
		// 竖排读取
		for(int i=0; i<colcount; i++) {
			for(int j=0; j<length/colcount; j++) {
				formatStr += charArray[j][i];
			}
			// 每排加入换行
			formatStr += "\n";
		}
		System.out.println(formatStr);
		return formatStr;
	}


	public static void main (String[] arg) {
		LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
		new LwjglApplication(new ChineseTestGame(), config);
	}
}